package com.lmb.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lmb.bean.Category;
import com.lmb.bean.Dish;
import com.lmb.bean.R;
import com.lmb.bean.Setmeal;
import com.lmb.dto.DishDto;
import com.lmb.dto.SetmealDto;
import com.lmb.service.CategoryService;
import com.lmb.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @projectName: reggie
 * @package: com.lmb.controller
 * @className: SetmalController
 * @author: Eric
 * @description: TODO
 * @date: 2022/9/20 14:57
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;


    @GetMapping("/page")
    public R pageList(Integer page,Integer pageSize,String name){
        Page<Setmeal> setmealPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.like(name!=null,Setmeal::getName,name);
        setmealService.page(setmealPage,setmealLambdaQueryWrapper);

        Page<SetmealDto> setmealDtoPage = new Page<>();
        BeanUtils.copyProperties(setmealPage,setmealDtoPage,"records");

        List<Setmeal> setmealList = setmealPage.getRecords();
        List<SetmealDto> setmealDtoList = new ArrayList<>();
        setmealList.stream().forEach(setmeal -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal,setmealDto);
            Category category = categoryService.getById(setmeal.getCategoryId());
            setmealDto.setCategoryName(category.getName());
            setmealDtoList.add(setmealDto);
        });
        setmealDtoPage.setRecords(setmealDtoList);
        return R.success(setmealDtoPage);
    }

    @PostMapping("/status")
    public R upStatus(Long[] ids , Integer status , HttpSession session){

        setmealService.upStatuss(ids,status,session);

        return R.success("状态修改成功");

    }

    @DeleteMapping
    public R deleteDish(Long[] ids){

        setmealService.deleteDishs(ids);

        return R.success("删除成功！！！");
    }

    @PostMapping
    public R addSetmeal(@RequestBody SetmealDto setmealDto,HttpSession session){
        System.out.println(setmealDto);
        System.out.println(setmealDto.getName());
        return setmealService.addSetmealAndSetmealDish(setmealDto, session)?
                R.success("添加成功"):R.error("添加失败");
    }

    @GetMapping("/list")
    public R setmealList(Long categoryId,Integer status){
        System.out.println(categoryId);
        System.out.println(status);
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(categoryId!=null,Setmeal::getCategoryId,categoryId);
        setmealLambdaQueryWrapper.eq(status!=null,Setmeal::getStatus,status);
        List<Setmeal> list = setmealService.list(setmealLambdaQueryWrapper);
        return R.success(list);
    }
}
